#include "EmotivProcessing.h"


EmotivProcessing::EmotivProcessing():Algorithm()
{
	//Apro la gui per la selezione della configurazione delle classificazioni
	myGui = new EmoGui(NULL,myClass,&demo);
	myGui->ShowModal();

	delete(myGui);
	myClass[4] = -1;	//x non classification

	//Open the device connection.
	eEvent	= EE_EmoEngineEventCreate();
	eState	= EE_EmoStateCreate();
	
	if (demo)
	{
		if (EE_EngineRemoteConnect("127.0.0.1", 1726) != EDK_OK)
		{
			Error = true;
			wxMessageBox(wxT("Error, Emotiv Engine start up failed."),wxT("Error"));
		}
		else
		{
			wxMessageBox(wxT("Please activate the Simulator devices to use Emo Composer"),wxT("Warning"));
			Error = false;
		}
	}
	else
	{
		if (EE_EngineConnect() != EDK_OK)
		{
			Error = true;
			wxMessageBox(wxT("Error, Emotiv Engine start up failed."),wxT("Error"));
		}
		else
			Error = false;
	}
	Open(10,10,16,4,1);
	//Error = false;
	SingleSample = false;
}

EmotivProcessing::~EmotivProcessing(void)
{
}

void EmotivProcessing::RunSecWin(void)
{	
	ShiftSecondaryWin(64);
}
void EmotivProcessing::RunMainWin(void)
{

	//QUI CONTROLLO SE ARRIVA QUALCOSA
	int state = EE_EngineGetNextEvent(eEvent);

	if (state == EDK_OK) 
	{

				EE_Event_t eventType = EE_EmoEngineEventGetType(eEvent);
				//EE_EmoEngineEventGetUserId(eEvent, &userID);

				// Log the EmoState if it has been updated
				if (eventType == EE_UserAdded) 
				{
					//Bello, ma non me ne faccio nulla
				}
				if (eventType == EE_EmoStateUpdated)
				{
					//Classificazione avenuta
					EE_EmoEngineEventGetEmoState(eEvent, eState);

					EE_CognitivAction_t actionType;
					actionType = ES_CognitivGetCurrentAction(eState);
					float actionPower;
					actionPower = ES_CognitivGetCurrentActionPower(eState);
					int myNewClass = CheckState(actionType);
					CheckClassification (myNewClass, actionPower);

				}
				if (eventType == EE_CognitivEvent)
				{
					//Solo per training, non serve
				}
				if (eventType == EE_ExpressivEvent)
				{
					//Solo per training, non serve
				}
			
	}
	Sleep(10);
	ShiftMainWin(256);
}


int EmotivProcessing::CheckState(EE_CognitivAction_t state)
{
//COG_NEUTRAL						= 0x0001,
//COG_PUSH						= 0x0002,
//COG_PULL						= 0x0004,
//COG_LIFT						= 0x0008,
//COG_DROP						= 0x0010,
//COG_LEFT						= 0x0020,
//COG_RIGHT						= 0x0040,
//COG_ROTATE_LEFT					= 0x0080,
//COG_ROTATE_RIGHT				= 0x0100,
//COG_ROTATE_CLOCKWISE			= 0x0200,
//COG_ROTATE_COUNTER_CLOCKWISE	= 0x0400,
//COG_ROTATE_FORWARDS				= 0x0800,
//COG_ROTATE_REVERSE				= 0x1000,
	//COG_DISAPPEAR					= 0x2000
	switch (state)
	{
	case COG_NEUTRAL:
		return -1;
		break;
	case COG_PUSH:
		return 0;
		break;
	case COG_PULL:
		return 1;
		break;
	case COG_LIFT:
		return 2;
		break;
	case COG_DROP:
		return 3;
		break;
	case COG_LEFT:
		return 4;
		break;
	case COG_RIGHT:
		return 5;
		break;
	case COG_ROTATE_LEFT:
		return 6;
		break;
	case COG_ROTATE_RIGHT:
		return 7;
		break;
	case COG_ROTATE_CLOCKWISE:
		return 8;
		break;
	case COG_ROTATE_COUNTER_CLOCKWISE:
		return 9;
		break;
	case COG_ROTATE_FORWARDS:
		return 10;
		break;
	case COG_ROTATE_REVERSE:
		return 11;
		break;
	case COG_DISAPPEAR:
		return 12;
		break;
	}
	return NULL;
}

void EmotivProcessing::CheckClassification(int mClass, float mPower)
{
	//Position and value of the feedback
	//Left - Right - Up - Down
	float Feed[4]={10,20,30,40};
	float Class = 4;
	int SetClass[5] = {16, 32, 1, 8, 4};

	ResetFeed(Feed);
	//UP
	for (int i=0; i<5; i++)
	{
		if (myClass[i] == mClass)
		{
			Class = SetClass[i];
			if (i!=4)
				Feed[i] = mPower*100;
		}
	}
	SetFeedback(Feed);
	SetClassification(Class);
}

void EmotivProcessing::ResetFeed(float *Feed)
{
	Feed[0] = 0;
	Feed[1] = 0;
	Feed[2] = 0;
	Feed[3] = 0;
}